<?php
//开启session
session_start();
//包含数据库配置文件
include '../public/dbconfig.php';
//包含函数库文件
include '../public/functions.php';
//调用函数 链接数据库
$link = con();

//接收用户信息
//接收用户名
$username = $_POST['username'];
//接收密码
$password = $_POST['password'];
//接收确认密码
$notpassword = $_POST['notpassword'];
//接收email
$email = $_POST['email'];
//接收验证码
$yzm = $_POST['yzm'];
//获取注册时间
$rtime = time();
//获取注册ip
$ip = $_SERVER['REMOTE_ADDR'];

//判断验证码是否一致
if($yzm != $_SESSION['yzmstr']){
	echo '<script>alert("验证码不正确");window.location.href="register.php";</script>';
	exit;
}

//判断用户名长度
$userlen = strlen($username);
if($userlen < 6 || $userlen > 12){
	echo '<script>alert("用户名长度必须是6-12个字符");window.location.href="register.php";</script>';
	exit;
}
//判断密码长度
$passlen = strlen($password);
if($passlen < 6 || $passlen > 18){
	echo '<script>alert("密码长度必须是6-18个字符");window.location.href="register.php";</script>';
	exit;
}
//判断密码和确认密码是否一致
if($password == $notpassword){
	$pass = md5($password);
}else{
	echo '<script>alert("密码和确认密码不一致");window.location.href="register.php";</script>';
	exit;
}
//判断ip 是否是 ::1 如果是就转换为 127.0.0.1
if($ip == '::1'){
	$rip = ip2long('127.0.0.1');
}else{
	$rip = ip2long($ip);
}

//准备sql
$sql = "insert into user(username,password,email,rtime,rip)values('{$username}','{$pass}','{$email}','{$rtime}','{$rip}')";
//执行sql
$res = mysqli_query($link, $sql);
//判断
if($res){
	$_SESSION['newid'] = mysqli_insert_id($link);
	echo '<script>alert("注册成功");window.location.href="index.php";</script>';
}else{
	echo '<script>alert("注册失败");window.location.href="register.php";</script>';
}
//关闭数据库链接
mysqli_close($link);